﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XMK.Utils;
using XMK.Framework.Model.Cond;
using XMK.Framework.Model;

namespace XMK.BatchImport
{
    public partial class TargetFrm : Form
    {
        public delegate void delSetTargetValue(string text,int value);
        public event delSetTargetValue targetevent;

        public TargetFrm()
        {
            InitializeComponent();
        }

        private void TreeBind() {
            List<TreeInfo> treeList = QueryTreeInfo();
            List<TreeInfo> rootList = treeList.Where(p=>p.p_obj_ID==0).ToList<TreeInfo>();
            for (int i = 0; i < rootList.Count; i++) {
                TreeNode treeNode = new TreeNode();
                treeNode.Name = rootList[i].obj_ID.ToString();
                treeNode.Text = rootList[i].obj_Name;
                SubTreeNodes(treeList,rootList[i].obj_ID, treeNode);
                targetTree.Nodes.Add(treeNode);                
            }
            targetTree.ImageList = imageList1;
            targetTree.ImageIndex = 0;
            targetTree.ExpandAll();
           
        }

        
        private static void  SubTreeNodes(List<TreeInfo> treeList, int parentId, TreeNode treeNode)
        {
            List<TreeInfo> sublist;
            sublist = treeList.Where(p => p.p_obj_ID == parentId).ToList<TreeInfo>();
            foreach (TreeInfo treeinfo in sublist)
            {
                TreeNode subtreeNode = new TreeNode();
                subtreeNode.Name = treeinfo.obj_ID.ToString();
                subtreeNode.Text = treeinfo.obj_Name;
                SubTreeNodes(treeList, treeinfo.obj_ID, subtreeNode);
                treeNode.Nodes.Add(subtreeNode);
            }
           
        }


        /// <summary>
        ///  取目标树
        /// </summary>
        /// <returns></returns>
        private static List<TreeInfo> QueryTreeInfo()
        {
            CommonDataCond cond = new CommonDataCond();

            cond.ws = new WsCond();
            cond.ws.wsId = "BATTARGETTREE";
            cond.ws.logId = "1111111";
            cond.ws.callDatetime = DateHelper.ConvertString(DateTime.Now, DateHelper.FORMAT_yyyy_MM_dd_HH_mm_ss_fff);
            cond.ws.ipAddr = "10.100.100.100";
            cond.ws.supplierId = "xmk";
            cond.cond = null;
            string s = JsonHelper.ToJson(cond);
            TadongWS.WSService service = new TadongWS.WSService();
            string retstr = service.callService(s);
            var commdatainfo = JsonHelper.FromJson<CommonDataInfo>(retstr);


            var list = (Newtonsoft.Json.Linq.JArray)(commdatainfo.list);
            List<TreeInfo> tlist = new List<TreeInfo>();
            foreach (Newtonsoft.Json.Linq.JObject tree in list)
            {
                tlist.Add(new TreeInfo { obj_ID = int.Parse(tree["obj_ID"].ToString()), obj_Name = tree["obj_Name"].ToString(), p_obj_ID = int.Parse(tree["p_obj_ID"].ToString()) });
            }

            return tlist;
        }

        /// <summary>
        /// 绑定树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TargetFrm_Load(object sender, EventArgs e)
        {
            TreeBind();
        }

        /// <summary>
        /// 选择目标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void targetTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (targetevent != null) { 
                TreeNode node=targetTree.SelectedNode;
                targetevent(node.Text,int.Parse(node.Name));
            }
        }
    }
}
